package com.cwd.arm.application.service.read;

import com.cwd.arm.common.dto.session.AuthDto;
import com.cwd.arm.infrastructure.dao.AuthDao;
import com.cwd.arm.infrastructure.po.AuthPo;
import com.cwd.arm.application.service.read.facade.AuthReadService;
import com.cwd.arm.infrastructure.query.AuthDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:权限
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Repository
@Primary
public class AuthReadServiceImpl implements AuthReadService {

    @Autowired
    private AuthDao authDao;

    @Override
    public AuthDto selectById(Long id) {
        return convertToDto(authDao.getById(id));
    }

    @Override
    public List<AuthDto> selectByUserId(Long userId) {
        return convertToDtoList(authDao.selectByUserId(userId));
    }

    @Override
    public List<AuthDto> selectByType(String type) {
        return convertToDtoList(authDao.selectByType(type));
    }

    @Override
    public List<AuthDto> selectByUserIdAndType(Long id, String type) {
        AuthDaoQuery authDaoQuery = new AuthDaoQuery();
        authDaoQuery.setAuthType(type);
        authDaoQuery.setRoleId(id);
        return convertToDtoList(authDao.selectByQuery(authDaoQuery));
    }

    private AuthDto convertToDto(AuthPo po) {
        AuthDto dto = new AuthDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<AuthDto> convertToDtoList(List<AuthPo> poList){
        return poList.stream().map(po->convertToDto(po)).collect(Collectors.toList());
    }

}
